<?php

if ( ! function_exists( 'wptovp_get_option' ) ) {
    function wptovp_get_option( $option = '', $default = null ) {
        $options = get_option( 'wptovp_' ); // Attention: Set your unique id of the framework
        return ( isset( $options[$option] ) ) ? $options[$option] : $default;
    }
}


if ( ! function_exists( 'wptovp_get_post_categories' ) ) {
	function wptovp_get_post_categories( $post_id ) {
		$categories     = get_the_category( $post_id );
		$category_names = array();
		foreach ( $categories as $category ) {
			$category_names[] = [ "id" => $category->term_id, "name" => $category->name ];
		}

		return $category_names;
	}
}

if ( ! function_exists( 'wptovp_save_sidebar_menu_and_top_menu' ) ) {
	function wptovp_save_sidebar_menu_and_top_menu( $top_menu = [], $sidebar_menu = [], $title, $description, $socialLinks = []) {
		$config = "import { defineConfig } from 'vitepress'
export default defineConfig({
  lang: 'en-US',
  title: $title,
  description: $description,
  themeConfig: {
    nav: $top_menu,
    sidebar: $sidebar_menu,
    socialLinks: $socialLinks,
  }
})';
	}
}";
		$global_config = wp_to_vp_plugin_path . 'vitepress/docs/.vitepress/config.mjs';

		if(file_exists($global_config)){
			unlink($global_config);
		}

		$handle = fopen( $global_config, 'a' );
	    fwrite( $handle, $config );
	    fclose( $handle );
	}
}